package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BaseParamBo;
import com.cskaoyan.bean.BaseRespData;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.admin.system.*;
import com.cskaoyan.service.admin.AdminService;
import com.cskaoyan.util.ValidationUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("admin/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    @RequiresPermissions(value = {"*", "admin:admin:admin"}, logical = Logical.OR)
    @GetMapping("list")
    public BaseRespVo list(String username, BaseParamBo param){

        BaseRespData data = adminService.query(username,param);
        return BaseRespVo.ok(data);
    }

    @RequiresPermissions(value = {"*", "admin:admin:create"}, logical = Logical.OR)
    @PostMapping("create")
    public BaseRespVo create(@Validated @RequestBody AddAdminsBo addAdminsBo, BindingResult bindingResult){

        //1 参数校验
        BaseRespVo baseRespVo = ValidationUtils.dealWithFieldError(bindingResult);

        if(baseRespVo.getErrno() == 500){
            return baseRespVo;
        }

        //2 业务逻辑

        //先判重复
        Boolean isUsernameDuplicated = adminService.usernameIsDuplicate(addAdminsBo);

        if(isUsernameDuplicated){
            //用户名重复
            return BaseRespVo.fail("管理员名称不能重复!");
        }
        AddAdminsVo addAdminsVo = adminService.createAdmin(addAdminsBo);

        return BaseRespVo.ok(addAdminsVo);
    }

    @RequiresPermissions(value = {"*", "admin:admin:delete"}, logical = Logical.OR)
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody ListAdminsVo adminsVo){

        int deleteCode = adminService.delete(adminsVo);

        if(deleteCode == 404){
            return BaseRespVo.fail("删除失败,请重试");
        }
        return BaseRespVo.ok();
    }

    @RequiresPermissions(value = {"*", "admin:admin:update"}, logical = Logical.OR)
    @PostMapping("update")
    public BaseRespVo update(@RequestBody @Validated ModifyAdminsBo modifyAdminsBo,BindingResult bindingResult){

        //参数校验
        BaseRespVo baseRespVo = ValidationUtils.dealWithFieldError(bindingResult);
        if(baseRespVo.getErrno() == 500){
            return baseRespVo;
        }

        Boolean isUsernameDuplicated = adminService.usernameIsDuplicate(modifyAdminsBo);

        if(isUsernameDuplicated){
            //用户名重复
            return BaseRespVo.fail("管理员名称不能重复!");
        }

        ModifyAdminsVo modifyAdminsVo = adminService.update(modifyAdminsBo);

        if(modifyAdminsVo == null){
            //新旧密码一致
            return BaseRespVo.fail("新旧密码一致,请重新输入!");
        }
        return BaseRespVo.ok(modifyAdminsVo);
    }
}
